function EngineSearch(){
	
	this.requestList = []; //list of events to search
	this.inProcess   = false;
	
	this.set = function(obj){
		this.inProcess = true; 
		this.requestList.push(obj);
	};
	
	this.reset = function(){
		this.requestList = [];
	};
	
	//used by engine.render
	this.processMouseInputEvents = function(ctx, polygonObj){
		for(var i=0; i<this.requestList.length; i++){
			if(ctx.isPointInPath(this.requestList[i].mouseX ,this.requestList[i].mouseY)){
				//this.requestList[i].activeSelection.push(polygonObj.id);
				this.requestList[i].activeSelection.push(polygonObj);
			}
		}
	};
	
	this.end = function(){
		if(this.inProcess){
			var pure = []; 
			for(var i=0; i<this.requestList.length; i++){
				if(this.requestList[i].ready == false){
					//exclude events that outside cube area
					if (this.requestList[i].saveSelection()){
						pure.push(this.requestList[i]);
					}
				}
			}
			this.requestList = pure;
			this.inProcess = false;
		}
	};

}


function SysMouseEvent(){
	this.id = null;
	this.eventType = null;
	this.mouseX = null;
	this.mouseY = null;
	this.ready = false;
	
	this.savedSelection = []; //search result
	this.activeSelection = []; //all polygons on selected x,y coordinats
	
	this.sortByAvg_z = function(a,b){
		return b.avg_z - a.avg_z;
	};
	
	this.saveSelection = function(){
		if(this.activeSelection.length < 1){
			this.savedSelection = [];
		}else{
			var temp = this.activeSelection;
			//this.activeSelection = [];
			//this.savedSelection = [];
			
			//this.savedSelection = this.activeSelection;
			//this.activeSelection = [];
			//var temp = this.savedSelection;
			
			//we want to get whole polygon object and not just its id
			/*for(var i=0; i < this.savedSelection.length; i++){
				for(var k=0; k < this.data.length; k++){
					if(this.data[k].id == this.savedSelection[i]){
						temp.push(this.data[k]);
					}
				}
			}
			*/
			temp.sort(this.sortByAvg_z);
			
			var realSelectedPolygon = temp[temp.length-1];
			//this.savedSelection = []; 
			this.savedSelection.push(temp[temp.length-1]);
			this.ready = true;
		}
		return this.ready;
	};
	
}

